/**
 * @author: shiding
 * @data: 2022/6/1 15:21
 **/
public class ReverseList {

    public static void main(String[] args) {

    }

    /**
     * 递归操作
     *
     * @param head
     * @return
     */
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode last = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return last;
    }

    /**
     * 迭代操作
     * 对 每个 节点 进行 每个节点
     *
     * @param head
     * @return
     */
    public ListNode reverseList1(ListNode head) {
        ListNode prev = null;
        ListNode node = head;
        while (node != null) {
            ListNode nextTemp = node.next;
            node.next = prev;
            prev = node;
            node = nextTemp;
        }
        return prev;
    }

    class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
